package fem2.jlinalg;

import fem2.enu.EchoLevelType;
import fem2.enu.SolverType;
import inf.jlinalg.GeneralMatrixLSESolver;
import inf.jlinalg.ILSESolver;
import inf.jlinalg.IMatrix;
import inf.jlinalg.QuadraticMatrixInfo;
import inf.jlinalg.SolveFailedException;
import inf.jlinalg.SpoolesLSESolver;

public class BaitschSolver extends Solver {

	ILSESolver solver;

	public BaitschSolver(SolverType type) {
		if (type == SolverType.BAITSCH_SOLVER_GENERIC) {
			solver = new GeneralMatrixLSESolver();
		} else if (type == SolverType.BAITSCH_SOLVER_SPOOLES) {
			solver = new SpoolesLSESolver();
		} else {
			throw new Error("invalid solver type");
		}
	}

	@Override
	public void setSize(int dimk) {
		QuadraticMatrixInfo aInfo = solver.getAInfo();
		aInfo.setSize(dimk);
		solver.initialize();
	}

	@Override
	public void setSize(SymbolicTable t) {
	}

	@Override
	public void solve(double[] b) throws SolveFailedException {
		if (getEchoLevel().contain(EchoLevelType.AUX1)) {
			long start = System.currentTimeMillis();
			solver.solve(b);
			long end = System.currentTimeMillis();
			System.out.println("Solving time: " + (end - start) + " ms.");
		} else {
			solver.solve(b);
		}
	}

	@Override
	public IMatrix getA() {
		return solver.getA();
	}

	@Override
	public void initialize() {
		solver.initialize();
	}

	@Override
	public void finalize() {
	}

}
